package com.ranshiyan.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 * 
 * @author zhuzg
 *
 */
public class DateUtils {
	
	/**
	 * 一天有多少毫秒
	 */
	private final static int MSECOND_PER_DAY = 1000*60*60*24; 

	/**
	 *  计算年龄
	 * @param birthday 生日
	 * @return
	 */
	public static int getAge(Date birthday) {
		
		//获取日历的实例  
		Calendar instance = Calendar.getInstance();
		int currYear =  instance.get(Calendar.YEAR);//得到当前年
		int currMonth =  instance.get(Calendar.MONTH);//得到当前月份
		int currDate =  instance.get(Calendar.DATE);//得到当前的日
		
		
		instance.setTime(birthday);
		int birthYear =  instance.get(Calendar.YEAR);//得到出生年
		int birthMonth =  instance.get(Calendar.MONTH);//得到出生月份
		int birthDate =  instance.get(Calendar.DATE);//得到出生日
		
		int age = currYear-birthYear;
		// 如果出生的月份大于当前的月份
		if(currMonth<birthMonth)
			age--;
		// 同月的情况下 需要比较出生的日期
		else if(currMonth==birthMonth && currDate<birthDate)
			age--;
		
		return age;
		
	}
	
	/**
	 * 计算到将来的某个日期还剩余多少天
	 * @param futureDate  未来的一个日期
	 * @return
	 */
	public static int  getRemainDays(Date futureDate) {
		
		//当前的日期
		Date currentDate = new Date();
		int days =  (int) ((futureDate.getTime() - currentDate.getTime())/MSECOND_PER_DAY);
		return days;
	}
	
	/**
	 * 判断两个日期是否为同一天
	 * @param date
	 * @return
	 */
	public static  boolean isCurrentDay(Date date) {
		
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		// 当前日期的字符串
		String currenStr = format.format(new Date());
		
		// 计算给定日期的字符串
		String dateStr = format.format(date);
		
		// 是否为当天
		return (dateStr.equals(currenStr));
		
	}
	
	/**
	 * 给定时间对象，初始化到该月初的1日0时0分0秒0毫秒
	 * @param date
	 */
	public static Date getBeginOfMonth(Date date) {
		
		// 得到日历的实例
		Calendar calendar = Calendar.getInstance();
		
		//设置时间
		calendar.setTime(date);
		
		calendar.set(Calendar.DAY_OF_MONTH, 1);// 1 日
		calendar.set(Calendar.AM_PM, Calendar.AM);
		calendar.set(Calendar.HOUR, 0);
		
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		
		return calendar.getTime();
		
	}
	
	/**
	 *  计算月末
	 * @param date
	 * @return
	 */
	public static Date getEndOfMonth(Date date) {
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		calendar.add(Calendar.MONTH, 1);// 月份加1
		
		calendar.set(Calendar.DAY_OF_MONTH, 1);// 1 日
		calendar.set(Calendar.AM_PM, Calendar.AM);
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);// 已经成为下一个月的月初了
		
		calendar.add(Calendar.SECOND, -1);// 下一个月的月初减去一秒变为当前月的月末
		
		return calendar.getTime();
		
	}
	
	/**
	 * 判断传入的日期 与当前的日期是不是同一周
	 * @param date
	 */
	public static boolean  isInWeek(Date date) {
		System.out.println("date is " + date);
		
		// 求本周的周一的日期
		Calendar calendar = Calendar.getInstance();
		
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);// 计算出今天是周几
		
		calendar.add(Calendar.DATE, -(dayOfWeek-1-1));// 移动到周一
		calendar.set(Calendar.AM_PM, Calendar.AM);
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);//
		calendar.set(Calendar.MILLISECOND, 0);//
		
		
		
		
		Date mondayDate = calendar.getTime();// 计算出来周一
		System.out.println("mondayDate is " + mondayDate);
		
		calendar.add(Calendar.DATE, 7);// 下周一
		Date nextMonday  = calendar.getTime();// 下周一
		System.out.println("nextMonday is " + nextMonday);
		System.out.println("mondayDate is " + mondayDate);
		
		System.out.println("date.getTime() > mondayDate.getTime()  is " + (date.getTime() > mondayDate.getTime() ));
		
		System.out.println("date.getTime()< nextMonday.getTime()  is  "  + (date.getTime()< nextMonday.getTime()));
		
		System.out.println(" date.getTime() " +date.getTime() );
		
		System.out.println(" mondayDate.getTime() " +mondayDate.getTime() );
		
		System.out.println(" nextMonday.getTime() " +nextMonday.getTime() );
		
		
		return (date.getTime() > mondayDate.getTime() 
				&& date.getTime()< nextMonday.getTime());
		
		
		
		
		
		
		
		
	}
	
}
